<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    title="新增聚合配置"
    @visible-change="handleVisibleChange"
    @ok="handleOK"
  >
    <div class="pt-3px pr-3px">
      <BasicForm @register="registerForm" :model="model" />
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, nextTick } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { schemas } from '/@/views/setting/add/schema';
  import { Setting } from '/@/api/monitor/entity/entity';
  import { addSetting } from '/@/api/monitor/setting';

  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    emits: ['exitAddSetting', 'register'],
    setup(props, { emit }) {
      let tempID = -1;
      const [registerForm, getFormProps] = useForm({
        labelWidth: 120,
        size: 'large',
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });

      const [register, { setModalProps, closeModal }] = useModalInner((data) => {
        data && onDataReceive(data);
      });

      function onDataReceive(data) {
        tempID = Number(data.currentRoute.params.id);
        setModalProps({ title: '新增聚合配置 Object ID-' + tempID });
      }

      async function handleOK() {
        let st: Setting = getFormProps.getFieldsValue();
        st.length = Number(st.length);
        st.object_id = tempID;
        st.is_enable = getFormProps.getFieldsValue().enable;
        closeModal();
        emit('exitAddSetting', await addSetting(st));
      }

      function handleVisibleChange(v) {
        v && props.userData && nextTick(() => onDataReceive(props.userData));
      }

      return {
        register,
        schemas,
        registerForm,
        handleVisibleChange,
        setModalProps,
        handleOK,
        getFormProps,
      };
    },
  });
</script>
